package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.MemoEmpAddDTO;
import org.jsola.hr.dto.MemoEmpUpdateDTO;
import org.jsola.hr.query.MemoEmpQuery;
import org.jsola.hr.service.IMemoEmpService;
import org.jsola.hr.vo.MemoEmpListVO;
import org.jsola.hr.vo.MemoEmpVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 备忘录相关员工
 *
 * @author zhr
 */
@RestController("hrMemoEmpController")
@RequestMapping(HrConstants.API_PATH + "/v1/memoEmps")
public class MemoEmpController implements IMemoEmpControllerAPI {

    @Autowired
    private IMemoEmpService memoEmpService;

    @Override
    @PostMapping
    public Result<MemoEmpVO> save(@Valid @RequestBody MemoEmpAddDTO memoEmpAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增备忘录相关员工权限校验

        return Result.success(
                memoEmpService.save(memoEmpAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody MemoEmpUpdateDTO memoEmpUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改备忘录相关员工权限校验

        int count = memoEmpService.update(memoEmpUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{memoEmpId}")
    public Result<Integer> delete(@PathVariable String memoEmpId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除备忘录相关员工权限校验

        int count = memoEmpService.deleteByIds(tokenUser, memoEmpId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] memoEmpIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除备忘录相关员工权限校验

        int count = memoEmpService.deleteByIds(tokenUser, memoEmpIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{memoEmpId}")
    public Result<MemoEmpVO> findById(@PathVariable String memoEmpId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看备忘录相关员工权限校验

        return Result.success(
            memoEmpService.selectById(memoEmpId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<MemoEmpListVO>> listAll(@Valid MemoEmpQuery memoEmpQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看备忘录相关员工权限校验

        return Result.success(
            memoEmpService.select(memoEmpQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<MemoEmpListVO>> page(@Valid MemoEmpQuery memoEmpQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            memoEmpService.selectPage(memoEmpQuery, tokenUser.getSiteId())
        );
    }


}

